package com.danskin.wechat.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.danskin.wechat.dao.GiftConfigDao;
import com.danskin.wechat.entity.GiftConfig;
import com.danskin.wechat.service.GiftConfigService;
import com.danskin.wechat.util.LogUtil;
import com.danskin.wechat.util.Page;

@Service
public class GiftConfigServiceImpl extends BaseServiceImpl implements GiftConfigService {

	@Autowired
	private GiftConfigDao giftConfigDao;

	@Override
	public List<GiftConfig> listPageGiftConfigList(Page page) {
		LogUtil.info("=========查询礼券列表参数：" + ToStringBuilder.reflectionToString(page));

		List<GiftConfig> giftConfigList = new ArrayList<GiftConfig>();

		try {
			giftConfigList = giftConfigDao.listPageGiftConfigList(page);
		} catch (Exception e) {
			LogUtil.error("========查询礼券列表异常", e);
		}
		return giftConfigList;
	}

	@Override
	public GiftConfig selectGiftConfigById(Integer id) {
		if (id != null) {
			LogUtil.info("=========根据主键查询礼券配置参数：id=" + id);
		}

		GiftConfig giftConfig = new GiftConfig();

		try {
			giftConfig = giftConfigDao.selectGiftConfigById(id);
		} catch (Exception e) {
			LogUtil.error("========根据主键查询礼券配置异常", e);
		}
		return giftConfig;
	}

	@Override
	public int insertGiftConfig(GiftConfig giftConfig) {
		LogUtil.info("=========新增礼券配置参数：" + ToStringBuilder.reflectionToString(giftConfig));

		int insertCount = 0;

		try {
			long sum = giftConfigDao.selectSumProbabilityByType(giftConfig);
			if (sum + giftConfig.getProbability().longValue() > 100) {
				return -1;
			}
			insertCount = giftConfigDao.insertGiftConfig(giftConfig);
		} catch (Exception e) {
			LogUtil.error("========新增礼券配置异常", e);
		}
		return insertCount;
	}

	@Override
	public int updateGiftConfig(GiftConfig giftConfig) {
		LogUtil.info("=======更新礼券配置参数：" + ToStringBuilder.reflectionToString(giftConfig));

		int updateCount = 0;

		try {
			long sum = giftConfigDao.selectSumProbabilityByType(giftConfig);
			if (sum + giftConfig.getProbability().longValue() > 100) {
				return -1;
			}
			updateCount = giftConfigDao.updateGiftConfig(giftConfig);
		} catch (Exception e) {
			LogUtil.error("=======更新礼券配置异常", e);
		}
		return updateCount;
	}

	@Override
	public int deleteGiftConfigById(Integer id) {
		if (id != null) {
			LogUtil.info("=======根据礼券ID删除礼券配置参数：id=" + id);
		}

		int deleteCount = 0;

		try {
			deleteCount = giftConfigDao.deleteGiftConfigById(id);
		} catch (Exception e) {
			LogUtil.error("=======根据礼券ID删除礼券配置异常", e);
		}
		return deleteCount;
	}

	@Override
	public List<GiftConfig> selectGiftConfigList(GiftConfig giftConfig) {
		LogUtil.info("=======根据条件查询礼券列表参数：" + ToStringBuilder.reflectionToString(giftConfig));

		List<GiftConfig> giftConfigList = new ArrayList<GiftConfig>();

		try {
			giftConfigList = giftConfigDao.selectGiftConfigList(giftConfig);
		} catch (Exception e) {
			LogUtil.error("=========根据条件查询礼券列表异常", e);
		}
		return giftConfigList;
	}
}
